package com.hj.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.github.pagehelper.PageInfo;
import com.hj.pojo.Score;
import com.hj.service.IScoreService;
import com.hj.vo.PageResult;
import com.hj.vo.Result;

@RestController
@RequestMapping(value = "/score")
public class ScoreController {

    @Autowired
    IScoreService scoreService;

    @GetMapping("/list")
    public Result list(@RequestParam(defaultValue = "1") Integer page
    		, @RequestParam(defaultValue = "3") Integer size
    		, Score score) {
    	
    	PageInfo<Score> pageInfo = scoreService.list(page, size, score);
    	PageResult pr = new PageResult();
    	pr.setData(pageInfo.getList());
    	pr.setRow(pageInfo.getTotal());
    	
    	Result result = new Result();
        result.setCode(200);
        result.setMsg("ok");
        result.setData(pr);
    	
        return result;
    }

    @PostMapping("/add")
    public Result add(@RequestBody Score score) {
        scoreService.add(score);
        Result result = new Result();
        result.setCode(200);
        result.setMsg("ok");
        return result;
    }
    
    @PutMapping("/update")
    public Result update(@RequestBody Score score) {
        scoreService.update(score);
        Result result = new Result();
        result.setCode(200);
        result.setMsg("ok");
        return result;
    }

    @DeleteMapping("/{sid}")
    public Result del(@PathVariable("sid") String id) {
        scoreService.delete(id);
        Result result = new Result();
        result.setCode(200);
        result.setMsg("ok");
        return result;
    }
    
    @GetMapping("/{sid}")
    public Result findOne(@PathVariable("sid")String id) throws Exception {
        Result result = new Result();
        result.setCode(200);
        result.setMsg("ok");
        
        if (id.equals("-1")) {
        	result.setCode(202);
            result.setMsg("非法参数");
            return result;
		}
        
        result.setData(scoreService.findOne(id));
        return result;
    }

}
